package com.castle.fortress.admin.oa.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.oa.entity.HolidayTypeEntity;
import com.castle.fortress.admin.oa.dto.HolidayTypeDto;
import com.castle.fortress.admin.oa.mapper.HolidayTypeMapper;
import com.castle.fortress.admin.oa.service.HolidayTypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 假期类型 服务实现类
 *
 * @author whc
 * @since 2022-11-03
 */
@Service
public class HolidayTypeServiceImpl extends ServiceImpl<HolidayTypeMapper, HolidayTypeEntity> implements HolidayTypeService {
    /**
    * 获取查询条件
    * @param holidayTypeDto
    * @return
    */
    public QueryWrapper<HolidayTypeEntity> getWrapper(HolidayTypeDto holidayTypeDto){
        QueryWrapper<HolidayTypeEntity> wrapper= new QueryWrapper();
        if(holidayTypeDto != null){
            HolidayTypeEntity holidayTypeEntity = ConvertUtil.transformObj(holidayTypeDto,HolidayTypeEntity.class);
            wrapper.like(StrUtil.isNotEmpty(holidayTypeEntity.getName()),"name",holidayTypeEntity.getName());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<HolidayTypeDto> pageHolidayType(Page<HolidayTypeDto> page, HolidayTypeDto holidayTypeDto) {
        QueryWrapper<HolidayTypeEntity> wrapper = getWrapper(holidayTypeDto);
        Page<HolidayTypeEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<HolidayTypeEntity> holidayTypePage=baseMapper.selectPage(pageEntity,wrapper);
        Page<HolidayTypeDto> pageDto = new Page(holidayTypePage.getCurrent(), holidayTypePage.getSize(),holidayTypePage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(holidayTypePage.getRecords(),HolidayTypeDto.class));
        return pageDto;
    }

    @Override
    public IPage<HolidayTypeDto> pageHolidayTypeExtends(Page<HolidayTypeDto> page, HolidayTypeDto holidayTypeDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        HolidayTypeEntity holidayTypeEntity = ConvertUtil.transformObj(holidayTypeDto,HolidayTypeEntity.class);
        List<HolidayTypeEntity> holidayTypeList=baseMapper.extendsList(pageMap,holidayTypeEntity);
        Long total = baseMapper.extendsCount(holidayTypeEntity);
        Page<HolidayTypeDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(holidayTypeList,HolidayTypeDto.class));
        return pageDto;
    }
    @Override
    public HolidayTypeDto getByIdExtends(Long id){
        HolidayTypeEntity  holidayTypeEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(holidayTypeEntity,HolidayTypeDto.class);
    }

    @Override
    public List<HolidayTypeDto> listHolidayType(HolidayTypeDto holidayTypeDto){
        QueryWrapper<HolidayTypeEntity> wrapper = getWrapper(holidayTypeDto);
        List<HolidayTypeEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,HolidayTypeDto.class);
    }
}

